"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : dolphin
Case Name   : 数据库编码格式ASCII格式验证字符串忽略尾部空格
Create At   : 2022.04.06
Owner       : z@wan005
Description :
    1、创建b库编码格式为ASCII并查询插件
    2、插入数据并执行筛选
    5、清理环境
Expect      :
    2、成功
    2、设置成功，参数生效
    3、环境清理成功
History     :
"""

import os
import unittest

from yat.test import Node
from yat.test import macro

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class CompatibilityTest01(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.common = Common()
        self.sh_primary = CommonSH('PrimaryDbUser')
        self.user_node = Node('PrimaryDbUser')
        self.constant = Constant()
        self.db_name = 'db_string_case55'

    def test_string(self):
        text = '-----step1:创建兼容b库的数据库;expect:成功-----'
        self.log.info(text)
        sql_cmd = f"drop database if exists {self.db_name};" \
                  f"create database {self.db_name} dbcompatibility ='B' " \
                  f"encoding = 'SQL_ASCII';"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd, dbname='postgres')
        self.log.info(sql_res)
        self.assertIn(self.constant.CREATE_DATABASE_SUCCESS, sql_res,
                      '执行失败' + text)
        sql_cmd = "select extname from pg_extension where extname ='dolphin';"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd, dbname=self.db_name)
        self.log.info(sql_res)
        self.assertIn('dolphin', sql_res, '插件加载失败' + text)

        text = '-----step2:向ASCII编码格式数据库插入数据并执行查询; expect:成功-----'
        self.log.info(text)
        sql = "drop table if exists t_string_0055 cascade;" \
              "create table t_string_0055(c_1 character varying(10)," \
              "c_2 varchar(10),c_3 varchar2(20),c_4 nvarchar2(20)," \
              "c_5 text,c_6 clob);" \
              "insert into t_string_0055 values('1aA干','1aA干','1aA干'," \
              "'1aA干','1aA干','1aA干'),('1aA干 ','1aA干 ','1aA干 '," \
              "'1aA干 ','1aA干 ','1aA干 '),('1aA干   ','1aA干   ','1aA干   '," \
              "'1aA干   ','1aA干   ','1aA干   ');" \
              "select c_1 from t_string_0055 where c_1='1aA干';" \
              "select c_2 from t_string_0055 where c_2='1aA干';" \
              "select c_3 from t_string_0055 where c_3='1aA干';" \
              "select c_4 from t_string_0055 where c_4='1aA干';" \
              "select c_5 from t_string_0055 where c_5='1aA干';" \
              "select c_6 from t_string_0055 where c_6='1aA干';"
        self.log.info(sql)
        sql_res = self.sh_primary.execut_db_sql(sql, dbname=self.db_name)
        self.log.info(sql_res)
        msg_lis = ['c_1    \n-----------\n 1aA干\n 1aA干 \n 1aA干   \n',
                   'c_2    \n-----------\n 1aA干\n 1aA干 \n 1aA干   \n',
                   'c_3    \n-----------\n 1aA干\n 1aA干 \n 1aA干   \n',
                   'c_4    \n-----------\n 1aA干\n 1aA干 \n 1aA干   \n',
                   'c_5    \n-----------\n 1aA干\n 1aA干 \n 1aA干   \n',
                   'c_6    \n-----------\n 1aA干\n 1aA干 \n 1aA干   \n']
        for msg in msg_lis:
            self.assertIn(msg, sql_res, '执行失败')

    def tearDown(self):
        self.log.info('-----step3:环境清理成功;expect:成功-----')
        sql = f"drop database if exists {self.db_name};"
        self.log.info(sql)
        res = self.sh_primary.execut_db_sql(sql)
        self.assertIn(self.constant.DROP_DATABASE_SUCCESS, res, '删除数据库失败')
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
